package history.hot100;

// 33. 搜索旋转排序数组:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
public class LeetCode_33 {
    public int search(int[] nums, int target) {
        if (nums == null || nums.length<=0) return -1;

        int start = 0;
        int end = nums.length-1;
        int mid;
        while(start <= end) {
            mid = start + ((end-start)>>1);
            if (nums[mid] == target) return mid;

            // 前半部分有序，注意此处用小于等于
            if (nums[start] <= nums[mid]) {
                // targt在前半部分
                if (target >= nums[start] && target < nums[mid]) {
                    end = mid-1;
                } else {
                    start = mid+1;
                }
            } else {
                if (target <= nums[end] && target > nums[mid]) {
                    start = mid+1;
                } else {
                    end = mid-1;
                }
            }
        }
        return -1;
    }
}
